<template>
    <el-card>
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
            <el-form-item label="分组ID" prop="groupId">
                <el-input v-model="queryParams.groupId" placeholder="分组ID" size="small"></el-input>
            </el-form-item>
            <el-form-item label="分组名称" prop="groupName">
                <el-input v-model="queryParams.groupName" placeholder="分组名称" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
        <el-row style="margin: 10px">
            <el-col :span="2">
                <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="分组ID" prop="groupId"></el-table-column>
            <el-table-column label="分组名称" prop="groupName"></el-table-column>
            <el-table-column label="插件名称" prop="pluginNames"></el-table-column>
            <el-table-column label="网关实例IP" prop="instanceIps"></el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
         <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
         </el-pagination>
    </el-card>
</template>

<script>
import {getGroupList,remove} from '@/api/getway/group.api'
    export default {
        data() {
            return {
                 tableData: [],
                 // 数据总条数
                 total: 0,
                 // 搜索
                 queryParams: {
                     current: 1,
                     size: 10,
                     groupId: undefined,
                     groupName: undefined
                 }
            }
        },
        mounted() {
            this.getList();
        },
        methods: {
            async getList() {
                const result = await getGroupList(this.queryParams);
                if(result.code === 200) {
                    this.tableData = result.data.records;
                    this.total = result.data.total
                }
            },
            handleAdd() {
                this.$router.push('/gateway/group/add')
            },
             handleDelete(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await remove(id);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                           this.getList();
                        }
                    }).catch((err) => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });          
                });
            },
            handleEdit(row) {
                this.$router.push({
                    path: '/gateway/group/edit',
                    query: {
                        id: row.id
                    }
                })
            },
            handleView(row) {
                this.$router.push({
                    path: '/group/view',
                    query: {
                        id: row.id
                    }
                })
            },
             // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            }
        }
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>